<?php
@set_magic_quotes_runtime(0);
function_exists('date_default_timezone_set') && date_default_timezone_set('Etc/GMT+0');
//防止外部访问包含文件的常量
define('IN_JBLOG',true);
//目录分隔符
define('DS', DIRECTORY_SEPARATOR);
//JBlog的根目录物理路径
define('JBLOG_ROOT', dirname(dirname(__FILE__)).DS);
//JBlog的include目录物理路径
define('JBLOG_INC', JBLOG_ROOT.'include'.DS);
//JBlog存放临时数据的data目录路径
define('DATA_ROOT',JBLOG_ROOT.'data'.DS);
//当前时间戳
define('NOW',time());

require JBLOG_INC.'db.php';

class Loader extends Base
{
    /**
     * Loader对象实例
     * @var object
     */
    private static $_instance;
    /**
     * JBlog配置信息
     * @var array
     */
    private $_config;
    
    /**
     * 数据库操作对象实例
     * @var object
     */
    private $_db;
    /**
     * 模块文件目录
     * @var string
     */
    private $_modpath;
    
    private function __construct() {
        $this->init_config();
        $this->init_db();
    }
    
    private function init_config() {
        $conf = JBLOG_ROOT . 'config.php';
        if (!is_file($conf)) {
            $this->redirect('./install');
        }
        include $conf;
        $this->_config = $config;
    }
    
    private function init_db() {
        DB::init($this->_config['database']);
    }
    
    
    private function init_input() {
        if (function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc()) {
            foreach (array('_GET','_POST','_COOKIE') as $var) {
                ${$var} = self::stripslashes_deep(${$var});
                reset(${$var});
            }
        }
    }
    
    public static function getInstance() {
        if (! (self::$_instance instanceof self)) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }
    
    /**
     * 开始路由分发
     */
    public function dispath() {
        
    }
    
    /**
     * 设置模块文件目录
     * @param string $path 
     */
    public function set_path($path) {
        $this->_modpath = $path;
    }
}

class ModuleBase extends Base 
{
    /**
     * 当前模块名称
     * @var string
     */
    public $modname;
    
    /**
     * 当前的操作名称
     * @var string
     */
    public $actname;
    
    final function __construct() {
        
    }
    
    
}

class Base {
    
    /**
     * 递归去除反斜杠
     * @param mixed $data
     * @return mixed
     */
    public static function stripslashes_deep($data) {
        return is_array($data) ? array_map(array('Base', 'stripslashes_deep'), $data) : stripslashes($data);
    }

    /**
     * URL跳转
     * @param string $url URL地址 
     */
    public static function redirect($url) {
        if ( !headers_sent() ) {
            header("HTTP/1.1 301 Moved Permanently");
            header('Location: '.$url);
        } else {
            echo '<script type="text/javascript">';
            echo 'window.location.href="'.$url.'";';
            echo '</script>';
            echo '<noscript>';
            echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
            echo '</noscript>';
        }    
    }
    
    public function showmsg($message = '', $jumpurl = '', $refreshtime = 3) {
        global $_USER;
        obclean();
        $refreshtime && $refreshtime *= 1000;
        if ( empty($jumpurl) ) {
            $jumpurl = $_USER['refer'] ? $_USER['refer'] : config('blogurl');
        }
        $message .= '<script>setTimeout("window.location.href=\''.$jumpurl.'\'", "'.$refreshtime.'")</script>';
        $message_title = __('JBlog 提示信息');
        $redirect = __('如果您的浏览器没自动跳转，请点击这里');
        $charset = JBLOG_CHARSET;
        if ( file_exists(THEME_ROOT.'show_msg.php') ) {
            template('show_msg');
            die;
        }
        echo <<<EOT
        <html>
        <head>
            <title>$message_title</title>
            <meta http-equiv="content-type" content="text/html; charset=$charset" />
        </head>
            <body style="background:#f0f0f0;text-align:center;font-family:Verdana;">
                <div style="background:#fff;width:500px;margin:0 auto;font-size:14px;">
                    <h1 style="font-size:16px;padding:8px;background:#F9FAF9;">$message_title</h1>
                    <div style="text-align:left;padding:8px;">$message</div>
                    <div style="padding:8px;"><a href="$jumpurl">$redirect</a></div>
                </div>
        </body>
        </html>	
EOT;
        exit();      
    }
    
    //输出404错误
    public function error_404() {
        if ( file_exists(THEME_ROOT.'404.php') ) {
            template('404');
        } else {
            header("HTTP/1.1 404 Not Found\n");
            header("Content-Type: text/html\n");
            header("Date: ".get_date('D, d M Y H:i:s',NOW)." GMT\n");
            echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"><html><head><title>404 Not Found</title></head>
            <body><h1>Not Found</h1>
            <p>The requested URL '.$_SERVER['SCRIPT_NAME'].' was not found on this server.</p>
            <p>Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.</p></body></html>';
        }
        exit;
    }
}
?>
